package com.kai.controller;

import com.kai.pojo.Dept;
import com.kai.pojo.Result;
import com.kai.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {

    //DI注入service
    @Autowired
    private DeptService deptService;


    @GetMapping("/depts")
    public Result list() {
        //调用service方法
       // System.out.println("查询全部部门数据");
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.findAll();

        //响应数据
        return Result.success(deptList);
    }


    //查询参数
    @DeleteMapping("/depts")
    public Result delete(Integer id) {
        //System.out.println("删除部门数据");
        log.info("删除部门数据");

        deptService.deleteById(id);
        return Result.success();
    }


    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept) {
       // System.out.println("接收json 新增部门数据");
        log.info("接收json 新增部门数据");
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result getDeptById(@PathVariable Integer id) {
        //System.out.println("根据路径参数 ID查询部门");
        log.info("根据路径参数 ID查询部门");
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result modifyDept(@RequestBody Dept dept){
        //System.out.println("接收json 修改部门");
        log.info("接收json 修改部门");
        deptService.modifyDept(dept);
        return Result.success();
    }


}
